Imports cmplib01

' what can the manager do?

' installations:
'OK   change ownership

' battlegroups:
'OK   change icon
'OK   change name
'OK   change status
'HOLD   change location
'   add/remove advantages

' units
'OK   change point value
'OK   change status (dead/not dead)


Public Class frmManager
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents lblSelectAChit As System.Windows.Forms.Label
    Friend WithEvents pnlTopBorder As System.Windows.Forms.Panel
    Friend WithEvents picCampaignerIcon As System.Windows.Forms.PictureBox
    Friend WithEvents txtTitleBox As System.Windows.Forms.TextBox
    Friend WithEvents tvwGroundChits As System.Windows.Forms.TreeView
    Friend WithEvents imlTreeIcons As System.Windows.Forms.ImageList
    Friend WithEvents lblProperties As System.Windows.Forms.Label
    Friend WithEvents lblFactionName As System.Windows.Forms.Label
    Friend WithEvents lblOwner As System.Windows.Forms.Label
    Friend WithEvents picIcon As System.Windows.Forms.PictureBox
    Friend WithEvents lblStatus As System.Windows.Forms.Label
    Friend WithEvents lblBGStatus As System.Windows.Forms.Label
    Friend WithEvents mnuPopup As System.Windows.Forms.ContextMenuStrip
    Friend WithEvents mnuOwner As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuIcon As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuName As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuStatus As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuAdvantages As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuPV As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents mnuFactionA As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents lblPV As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents mnuFactionB As System.Windows.Forms.ToolStripMenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmManager))
        Me.btnOK = New System.Windows.Forms.Button
        Me.tvwGroundChits = New System.Windows.Forms.TreeView
        Me.imlTreeIcons = New System.Windows.Forms.ImageList(Me.components)
        Me.lblSelectAChit = New System.Windows.Forms.Label
        Me.pnlTopBorder = New System.Windows.Forms.Panel
        Me.picCampaignerIcon = New System.Windows.Forms.PictureBox
        Me.txtTitleBox = New System.Windows.Forms.TextBox
        Me.lblProperties = New System.Windows.Forms.Label
        Me.lblOwner = New System.Windows.Forms.Label
        Me.lblFactionName = New System.Windows.Forms.Label
        Me.picIcon = New System.Windows.Forms.PictureBox
        Me.lblStatus = New System.Windows.Forms.Label
        Me.lblBGStatus = New System.Windows.Forms.Label
        Me.mnuPopup = New System.Windows.Forms.ContextMenuStrip(Me.components)
        Me.mnuOwner = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuFactionA = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuFactionB = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuIcon = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuName = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuStatus = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuAdvantages = New System.Windows.Forms.ToolStripMenuItem
        Me.mnuPV = New System.Windows.Forms.ToolStripMenuItem
        Me.lblPV = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.pnlTopBorder.SuspendLayout()
        CType(Me.picCampaignerIcon, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.picIcon, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.mnuPopup.SuspendLayout()
        Me.SuspendLayout()
        '
        'btnOK
        '
        Me.btnOK.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnOK.BackColor = System.Drawing.Color.LightSkyBlue
        Me.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnOK.ForeColor = System.Drawing.Color.Black
        Me.btnOK.Location = New System.Drawing.Point(337, 343)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.Size = New System.Drawing.Size(75, 23)
        Me.btnOK.TabIndex = 27
        Me.btnOK.Text = "OK"
        Me.btnOK.UseVisualStyleBackColor = False
        '
        'tvwGroundChits
        '
        Me.tvwGroundChits.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.tvwGroundChits.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.tvwGroundChits.ImageIndex = 0
        Me.tvwGroundChits.ImageList = Me.imlTreeIcons
        Me.tvwGroundChits.Location = New System.Drawing.Point(14, 70)
        Me.tvwGroundChits.Name = "tvwGroundChits"
        Me.tvwGroundChits.SelectedImageIndex = 0
        Me.tvwGroundChits.Size = New System.Drawing.Size(192, 259)
        Me.tvwGroundChits.TabIndex = 4
        '
        'imlTreeIcons
        '
        Me.imlTreeIcons.ImageStream = CType(resources.GetObject("imlTreeIcons.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.imlTreeIcons.TransparentColor = System.Drawing.Color.Transparent
        Me.imlTreeIcons.Images.SetKeyName(0, "installation.bmp")
        Me.imlTreeIcons.Images.SetKeyName(1, "battlegroup.PNG")
        Me.imlTreeIcons.Images.SetKeyName(2, "unit.PNG")
        Me.imlTreeIcons.Images.SetKeyName(3, "lilSkull.bmp")
        Me.imlTreeIcons.Images.SetKeyName(4, "advantage.PNG")
        '
        'lblSelectAChit
        '
        Me.lblSelectAChit.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblSelectAChit.BackColor = System.Drawing.Color.DarkSeaGreen
        Me.lblSelectAChit.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblSelectAChit.ForeColor = System.Drawing.Color.Black
        Me.lblSelectAChit.Location = New System.Drawing.Point(14, 49)
        Me.lblSelectAChit.Name = "lblSelectAChit"
        Me.lblSelectAChit.Size = New System.Drawing.Size(192, 18)
        Me.lblSelectAChit.TabIndex = 3
        Me.lblSelectAChit.Text = "Select A Chit"
        Me.lblSelectAChit.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'pnlTopBorder
        '
        Me.pnlTopBorder.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.pnlTopBorder.BackColor = System.Drawing.Color.SteelBlue
        Me.pnlTopBorder.Controls.Add(Me.picCampaignerIcon)
        Me.pnlTopBorder.Controls.Add(Me.txtTitleBox)
        Me.pnlTopBorder.Location = New System.Drawing.Point(9, 6)
        Me.pnlTopBorder.Name = "pnlTopBorder"
        Me.pnlTopBorder.Size = New System.Drawing.Size(416, 40)
        Me.pnlTopBorder.TabIndex = 25
        '
        'picCampaignerIcon
        '
        Me.picCampaignerIcon.Image = CType(resources.GetObject("picCampaignerIcon.Image"), System.Drawing.Image)
        Me.picCampaignerIcon.Location = New System.Drawing.Point(9, 11)
        Me.picCampaignerIcon.Name = "picCampaignerIcon"
        Me.picCampaignerIcon.Size = New System.Drawing.Size(16, 16)
        Me.picCampaignerIcon.TabIndex = 9
        Me.picCampaignerIcon.TabStop = False
        '
        'txtTitleBox
        '
        Me.txtTitleBox.BackColor = System.Drawing.Color.SteelBlue
        Me.txtTitleBox.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.txtTitleBox.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtTitleBox.ForeColor = System.Drawing.Color.WhiteSmoke
        Me.txtTitleBox.Location = New System.Drawing.Point(32, 8)
        Me.txtTitleBox.Multiline = True
        Me.txtTitleBox.Name = "txtTitleBox"
        Me.txtTitleBox.ReadOnly = True
        Me.txtTitleBox.Size = New System.Drawing.Size(312, 26)
        Me.txtTitleBox.TabIndex = 8
        Me.txtTitleBox.TabStop = False
        Me.txtTitleBox.Text = "Manager"
        '
        'lblProperties
        '
        Me.lblProperties.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblProperties.BackColor = System.Drawing.Color.DarkSeaGreen
        Me.lblProperties.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblProperties.ForeColor = System.Drawing.Color.Black
        Me.lblProperties.Location = New System.Drawing.Point(212, 49)
        Me.lblProperties.Name = "lblProperties"
        Me.lblProperties.Size = New System.Drawing.Size(202, 18)
        Me.lblProperties.TabIndex = 36
        Me.lblProperties.Text = "Properties"
        Me.lblProperties.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'lblOwner
        '
        Me.lblOwner.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblOwner.BackColor = System.Drawing.Color.DarkSeaGreen
        Me.lblOwner.Location = New System.Drawing.Point(213, 125)
        Me.lblOwner.Name = "lblOwner"
        Me.lblOwner.Size = New System.Drawing.Size(199, 17)
        Me.lblOwner.TabIndex = 0
        Me.lblOwner.Text = "Owning Faction"
        Me.lblOwner.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'lblFactionName
        '
        Me.lblFactionName.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblFactionName.BackColor = System.Drawing.Color.Gainsboro
        Me.lblFactionName.Location = New System.Drawing.Point(216, 143)
        Me.lblFactionName.Name = "lblFactionName"
        Me.lblFactionName.Size = New System.Drawing.Size(196, 15)
        Me.lblFactionName.TabIndex = 1
        Me.lblFactionName.Text = "<faction name>"
        '
        'picIcon
        '
        Me.picIcon.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.picIcon.BackColor = System.Drawing.Color.Black
        Me.picIcon.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None
        Me.picIcon.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.picIcon.Location = New System.Drawing.Point(212, 70)
        Me.picIcon.Name = "picIcon"
        Me.picIcon.Size = New System.Drawing.Size(141, 48)
        Me.picIcon.TabIndex = 6
        Me.picIcon.TabStop = False
        '
        'lblStatus
        '
        Me.lblStatus.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblStatus.BackColor = System.Drawing.Color.Gainsboro
        Me.lblStatus.Location = New System.Drawing.Point(216, 185)
        Me.lblStatus.Name = "lblStatus"
        Me.lblStatus.Size = New System.Drawing.Size(196, 15)
        Me.lblStatus.TabIndex = 38
        Me.lblStatus.Text = "<status>"
        '
        'lblBGStatus
        '
        Me.lblBGStatus.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblBGStatus.BackColor = System.Drawing.Color.DarkSeaGreen
        Me.lblBGStatus.Location = New System.Drawing.Point(213, 167)
        Me.lblBGStatus.Name = "lblBGStatus"
        Me.lblBGStatus.Size = New System.Drawing.Size(199, 17)
        Me.lblBGStatus.TabIndex = 37
        Me.lblBGStatus.Text = "Status"
        Me.lblBGStatus.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'mnuPopup
        '
        Me.mnuPopup.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuOwner, Me.mnuIcon, Me.mnuName, Me.mnuStatus, Me.mnuAdvantages, Me.mnuPV})
        Me.mnuPopup.Name = "mnuPopup"
        Me.mnuPopup.ShowImageMargin = False
        Me.mnuPopup.Size = New System.Drawing.Size(175, 136)
        '
        'mnuOwner
        '
        Me.mnuOwner.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuFactionA, Me.mnuFactionB})
        Me.mnuOwner.Name = "mnuOwner"
        Me.mnuOwner.Size = New System.Drawing.Size(174, 22)
        Me.mnuOwner.Text = "Change Owning Faction"
        '
        'mnuFactionA
        '
        Me.mnuFactionA.Name = "mnuFactionA"
        Me.mnuFactionA.Size = New System.Drawing.Size(130, 22)
        Me.mnuFactionA.Text = "Faction A"
        '
        'mnuFactionB
        '
        Me.mnuFactionB.Name = "mnuFactionB"
        Me.mnuFactionB.Size = New System.Drawing.Size(130, 22)
        Me.mnuFactionB.Text = "Faction B"
        '
        'mnuIcon
        '
        Me.mnuIcon.Name = "mnuIcon"
        Me.mnuIcon.Size = New System.Drawing.Size(174, 22)
        Me.mnuIcon.Text = "Change Icon"
        '
        'mnuName
        '
        Me.mnuName.Name = "mnuName"
        Me.mnuName.Size = New System.Drawing.Size(174, 22)
        Me.mnuName.Text = "Change Name"
        '
        'mnuStatus
        '
        Me.mnuStatus.Name = "mnuStatus"
        Me.mnuStatus.Size = New System.Drawing.Size(174, 22)
        Me.mnuStatus.Text = "Change Status"
        '
        'mnuAdvantages
        '
        Me.mnuAdvantages.Name = "mnuAdvantages"
        Me.mnuAdvantages.Size = New System.Drawing.Size(174, 22)
        Me.mnuAdvantages.Text = "Edit Advantages"
        Me.mnuAdvantages.Visible = False
        '
        'mnuPV
        '
        Me.mnuPV.Name = "mnuPV"
        Me.mnuPV.Size = New System.Drawing.Size(174, 22)
        Me.mnuPV.Text = "Change Point Value"
        '
        'lblPV
        '
        Me.lblPV.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblPV.BackColor = System.Drawing.Color.Gainsboro
        Me.lblPV.Location = New System.Drawing.Point(215, 226)
        Me.lblPV.Name = "lblPV"
        Me.lblPV.Size = New System.Drawing.Size(196, 15)
        Me.lblPV.TabIndex = 40
        Me.lblPV.Text = "<pv>"
        '
        'Label2
        '
        Me.Label2.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Label2.BackColor = System.Drawing.Color.DarkSeaGreen
        Me.Label2.Location = New System.Drawing.Point(212, 208)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(199, 17)
        Me.Label2.TabIndex = 39
        Me.Label2.Text = "Point Value"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'frmManager
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.Black
        Me.ClientSize = New System.Drawing.Size(424, 373)
        Me.Controls.Add(Me.lblPV)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.lblStatus)
        Me.Controls.Add(Me.lblBGStatus)
        Me.Controls.Add(Me.lblProperties)
        Me.Controls.Add(Me.picIcon)
        Me.Controls.Add(Me.lblFactionName)
        Me.Controls.Add(Me.lblOwner)
        Me.Controls.Add(Me.btnOK)
        Me.Controls.Add(Me.pnlTopBorder)
        Me.Controls.Add(Me.tvwGroundChits)
        Me.Controls.Add(Me.lblSelectAChit)
        Me.Name = "frmManager"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.pnlTopBorder.ResumeLayout(False)
        Me.pnlTopBorder.PerformLayout()
        CType(Me.picCampaignerIcon, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.picIcon, System.ComponentModel.ISupportInitialize).EndInit()
        Me.mnuPopup.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private ptrGameData As CampaignerGameData
    Private ptrSelectedChit As ChitMember

    Const c_Module As String = "frmManager"

    Public Sub New(ByRef Game As CampaignerGameData)
        Me.New()
        ptrGameData = Game
        ListChits()
    End Sub

    Private Sub ListChits()
        ' list objectives, then units, then tokens 

        Try
            tvwGroundChits.Nodes.Clear()
        Catch
            ' ahahahahha
        End Try
        ListByClass(GroundChitClass_cgd.Objective)
        ListByClass(GroundChitClass_cgd.Battlegroup)
        'ListByClass(GroundChitClass_cgd.Token)

    End Sub

    Private Sub ListByClass(ByVal GroundClass As GroundChitClass_cgd)
        Dim ico As Int16   '// this is the icon image
        Dim mem As ChitMember

        For Each mem In ptrGameData.Chits
            Dim gcNode As New CampaignerItemTreeNode
            gcNode.Text = mem.Name
            gcNode.Item = mem
            gcNode.ItemType = GroundClass
            Select Case mem.UnitType
                Case GroundChitClass_cgd.Objective : ico = 0
                Case GroundChitClass_cgd.Unit : ico = 2
                Case GroundChitClass_cgd.Battlegroup : ico = 1
                Case GroundChitClass_cgd.Token : ico = 3
            End Select

            If mem.Status = GroundUnitStatus_cgd.Dead Then
                ico = 3
            End If

            gcNode.ImageIndex = ico
            gcNode.SelectedImageIndex = ico

            If mem.UnitType = GroundClass Then
                If ptrGameData.selectedPlayer.PlayerName = "<Moderator>" Then
                    If mem.UnitType = GroundChitClass_cgd.Battlegroup Then
                        tvwGroundChits.Nodes.Add(chitmemberNode(mem))

                    ElseIf mem.UnitType = GroundChitClass_cgd.Objective Then
                        tvwGroundChits.Nodes.Add(gcNode)
                    End If

                Else
                    If mem.OwningFaction.FactionID = ptrGameData.selectedFaction.FactionID Then
                        '// its on the same faction at least, that means installations can be listed
                        If mem.UnitType = GroundChitClass_cgd.Objective Then
                            tvwGroundChits.Nodes.Add(gcNode)
                        ElseIf mem.UnitType = GroundChitClass_cgd.Battlegroup Then
                            '// check to see if its the owning player.
                            If mem.OwningPlayer.Equals(ptrGameData.selectedPlayer) Then
                                '// add all chitmember nodes
                                tvwGroundChits.Nodes.Add(chitmemberNode(mem))
                            End If
                        End If
                    End If
                End If

            End If

        Next

    End Sub

    Private Function chitmemberNode(ByRef bg As BattlegroupChit) As CampaignerItemTreeNode
        Try
            Dim bgNode As New CampaignerItemTreeNode
            Dim b As Byte

            bgNode.Text = bg.Name
            bgNode.Item = bg
            bgNode.ItemType = CampaignerItem_cgd.Battlegroup

            bgNode.ImageIndex = 1
            bgNode.SelectedImageIndex = 1

            If bg.Status = GroundUnitStatus_cgd.Dead Then
                bgNode.ImageIndex = 3
                bgNode.SelectedImageIndex = 3
            End If

            If bg.UnitType = GroundChitClass_cgd.Battlegroup Then
                '// add my advantages??
                Try
                    If bg.Advantages Is Nothing Then
                        ReDim bg.Advantages(5)
                        For b = 0 To 5
                            bg.Advantages(b) = New Advantage
                        Next
                    End If
                    '// list the advantages
                    For b = 0 To bg.Advantages.GetUpperBound(0)
                        Dim advNode As New CampaignerItemTreeNode
                        advNode.ItemType = CampaignerItem_cgd.Advantage
                        advNode.Item = bg.Advantages(b)
                        advNode.Text = bg.Advantages(b).name
                        advNode.ImageIndex = 4
                        advNode.SelectedImageIndex = 5
                        '// for test we'll make them all show
                        If bg.Advantages(b).name <> "N/A" Then bgNode.Nodes.Add(advNode)
                    Next

                Catch ex As Exception
                    HandleException("frmManager.chitmembernode[listAdvantages]", ex)
                End Try
            End If

            For Each member As ChitMember In bg.members
                If member.UnitType = GroundChitClass_cgd.Battlegroup Then
                    bgNode.Nodes.Add(chitmemberNode(member))
                Else
                    Dim unitnode As New CampaignerItemTreeNode
                    unitnode.Item = member
                    unitnode.ItemType = CampaignerItem_cgd.Unit
                    unitnode.Text = member.Name

                    If member.Status = GroundUnitStatus_cgd.Dead Then
                        unitnode.ImageIndex = 3
                        unitnode.SelectedImageIndex = 3
                    Else
                        unitnode.ImageIndex = 2
                        unitnode.SelectedImageIndex = 2
                    End If

                    bgNode.Nodes.Add(unitnode)

                    '// ELEMENTS!!! :D

                End If

            Next

            Return bgNode

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Function

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.Hide()
    End Sub

    Private Sub tvwGroundChits_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvwGroundChits.AfterSelect
        ' when we select something, check it and populate the actions we can take.
        Dim gcNode As CampaignerItemTreeNode = e.Node
        ' set the selected chit to the one selected in the treeview
        ptrSelectedChit = gcNode.Item

        ' list the selected chit's properties
        ListProperties()
    End Sub

    Private Sub RenameChit()
        ' give the good ole basic entry.
        Dim fEntry As New frmBasicEntry("Enter a new name.", "[a-z]")
        fEntry.ShowDialog()

        If fEntry.Cancelled = False Then
            Dim newName As String = fEntry.Value
            ptrSelectedChit.Name = newName
        End If

    End Sub

    Private Sub DeleteChit()
        If MessageBox.Show("Delete Chit?", "Are you sure you wish to completely remove this chit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            ptrGameData.Chits.remove(ptrSelectedChit)
            ListChits()
        End If

    End Sub

    Private Sub KillChit()
        ' converts the chit to dead status. lol. DEAD!!!
        ptrSelectedChit.Status = GroundUnitStatus_cgd.Dead

    End Sub

    Private Sub ReassignOwner()
        Dim fP As New frmPlayerSelect(ptrGameData)
        fP.ShowDialog()

        Try
            If fP.Cancelled = False Then
                ' reassign the chit
                ptrSelectedChit.OwningPlayer = fP.SelectedPlayer
                ' oooo.... what about the faction id eh?
                Dim fact As Faction
                fact = ptrGameData.Faction_g.Item(fP.SelectedPlayer.FactionKey)
                ptrSelectedChit.OwningFaction = fact
            End If
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub ChangePointValue()

        Dim sPat As String = "\b[0-9]" ' only numbers
        Dim fNewVal As New frmBasicEntry("Enter Numeric Value", sPat)

        fNewVal.ShowDialog()

        If Not fNewVal.Cancelled Then
            ' set the point value
            'ptrSelectedChit.c= fNewVal.Value
            DirectCast(ptrSelectedChit, BattlegroupChit).CurrentPV = fNewVal.Value
        End If

    End Sub

    Private Sub EditElements()

        Try
            Dim fElemManager As New frmElementManager(ptrGameData, ptrSelectedChit)
            fElemManager.ShowDialog()
        Catch ex As Exception
            HandleException("frmManager.EditElements", ex)
        End Try

    End Sub

    Private Sub ListProperties()
        Try

            Try
                lblFactionName.Text = ptrSelectedChit.OwningFaction.FactionName
            Catch ex As Exception
                'try to get the parent if it exists... xxP
                Dim par As CampaignerItemTreeNode = DirectCast(Me.tvwGroundChits.SelectedNode.Parent, CampaignerItemTreeNode)
                If Not par Is Nothing Then
                    lblFactionName.Text = DirectCast(par.Item, BattlegroupChit).OwningFaction.FactionName
                End If
            End Try

            lblStatus.Text = ptrSelectedChit.Status.ToString

            Select Case ptrSelectedChit.UnitType
                Case GroundChitClass_cgd.Battlegroup
                    lblPV.Text = DirectCast(ptrSelectedChit, cmplib01.BattlegroupChit).CurrentPV
                Case GroundChitClass_cgd.Unit
                    If DirectCast(ptrSelectedChit, ChitUnitTypeMember).OverridePointValue = 0 Then
                        lblPV.Text = DirectCast(ptrSelectedChit, ChitUnitTypeMember).ptrUnitType.SuggestedPointValue
                    Else
                        lblPV.Text = DirectCast(ptrSelectedChit, ChitUnitTypeMember).OverridePointValue
                    End If
            End Select

            '// lets do the advantages
            'ListAdvantages()

            Try
                Select Case ptrSelectedChit.UnitType
                    Case GroundChitClass_cgd.Battlegroup
                        picIcon.BackgroundImage = ptrSelectedChit.icon
                    Case GroundChitClass_cgd.Objective
                        picIcon.BackgroundImage = DirectCast(ptrSelectedChit, InstallationChit).ptrInstallation.Icon("OWNER")
                    Case GroundChitClass_cgd.Unit
                        picIcon.BackgroundImage = DirectCast(ptrSelectedChit, ChitUnitTypeMember).ptrUnitType.Icon("OWNER")
                End Select
            Catch ex As Exception

            End Try

        Catch ex As Exception
            HandleException("frmManager.listProperties", ex)
        End Try
    End Sub

    'Private Sub ListAdvantages()
    '    Try

    '        '// clear the old ones
    '        For i As Integer = 0 To advantageLabel.GetUpperBound(0)
    '            advantageLabel(i).Text = "N/A"
    '        Next

    '        If ptrSelectedChit.UnitType <> GroundChitClass_cgd.Battlegroup Then Exit Sub

    '        Dim bg As BattlegroupChit = ptrSelectedChit

    '        If bg.Advantages Is Nothing Then
    '            ReDim bg.Advantages(5)
    '            Dim noExpire As New NonExpiringContract

    '            For i As Integer = 0 To bg.Advantages.GetUpperBound(0)
    '                bg.Advantages(i) = New Advantage
    '                bg.Advantages(i).name = "<none>"
    '                bg.Advantages(i).description = "<no advantage>"
    '            Next

    '        End If

    '        For i As Integer = 0 To bg.Advantages.GetUpperBound(0)
    '            ListAdvantage(bg.Advantages(i), advantageLabel(i))
    '        Next

    '    Catch ex As Exception
    '        HandleException("frmManager.ListAdvantages", ex)
    '    End Try
    'End Sub

    Private Sub ListAdvantage(ByVal value As Advantage, ByRef lab As Label)
        Try
            lab.Text = value.name
        Catch ex As Exception
            HandleException("frmManager.ListAdvantage", ex)
        End Try
    End Sub

    Private Sub tvwGroundChits_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvwGroundChits.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Right And Not (ptrSelectedChit Is Nothing) Then
            '// hide all menuitems first
            Me.mnuIcon.Visible = False
            Me.mnuName.Visible = False
            Me.mnuOwner.Visible = False
            Me.mnuPopup.Visible = False
            Me.mnuPV.Visible = False
            Me.mnuStatus.Visible = False
            Me.mnuAdvantages.Visible = False

            '// determine which items should be shown
            Select Case ptrSelectedChit.UnitType
                Case GroundChitClass_cgd.Objective
                    mnuOwner.Visible = True
                    mnuStatus.Visible = True

                Case GroundChitClass_cgd.Battlegroup
                    'mnuOwner.Visible = True
                    mnuStatus.Visible = True
                    mnuIcon.Visible = True
                    mnuName.Visible = True
                    mnuPV.Visible = True
                    mnuAdvantages.Visible = True

                Case GroundChitClass_cgd.Unit
                    mnuStatus.Visible = True
                    mnuPV.Visible = True
            End Select
            mnuPopup.Show(tvwGroundChits, e.Location)
        End If
    End Sub

    Private Sub mnuStatus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuStatus.Click
        Dim f As New camp128c.frmChitmemberStatusSelector
        f.showdialog(Me.ptrGameData, ptrSelectedChit)
        ListProperties()
    End Sub

    Private Sub mnuIcon_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuIcon.Click
        Dim f As New OpenFileDialog
        f.Filter = "picture files |*.png; *.bmp; *.jpg"

        f.ShowDialog()

        Try
            If f.FileName <> "" Then
                Dim b As Bitmap = Bitmap.FromFile(f.FileName)
                ptrSelectedChit.icon = b
                '// log event??
                ptrGameData.LogEvents.Add(New CampaignerLogEvent(ptrSelectedChit.Name & " icon changed.", ptrGameData.selectedPlayer.Clone, ptrGameData.ElapsedTime.ToString, "<none>", EventAuthorityLevel.PublicEvent))
                ListProperties()
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub mnuName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuName.Click
        Try
            Dim f As New frmBasicEntry("Enter New Name", "")
            f.ShowDialog()

            If f.Cancelled Then Exit Sub

            Dim oldname As String = ptrSelectedChit.Name

            ptrSelectedChit.Name = f.Data

            tvwGroundChits.SelectedNode.Text = f.Data

            '// add the log event
            ptrGameData.LogEvents.Add(New CampaignerLogEvent(oldname & " was renamed to '" & f.Data & "'", ptrGameData.selectedPlayer.PlayerName, ptrGameData.ElapsedTime.ToString, "<none>", EventAuthorityLevel.PublicEvent))

        Catch ex As Exception
            HandleException("frmManager.mnuName_click", ex)
        End Try

    End Sub

    Private Sub mnuFactionA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFactionA.Click
        Try
            '// transfers control of the installation to THIS faction
            Dim fkey As String
            For Each fkey In ptrGameData.Faction_g.HT.Keys
                Dim fact As Faction = ptrGameData.Faction_g.Item(fkey)

                If Not ptrSelectedChit.OwningFaction.Equals(fact) Then
                    ptrGameData.LogEvents.Add(New CampaignerLogEvent(ptrSelectedChit.Name & " changed ownership to " & fact.FactionName, ptrGameData.selectedPlayer.PlayerName, ptrGameData.ElapsedTime.ToString, "<none>", EventAuthorityLevel.PublicEvent))
                    transferOwner(fact)
                End If

                Exit For
            Next

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub mnuFactionB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFactionB.Click
        Try
            Dim fkey As String
            Dim fact As Faction

            For Each fkey In ptrGameData.Faction_g.HT.Keys
                fact = ptrGameData.Faction_g.Item(fkey)
            Next

            If Not ptrSelectedChit.OwningFaction.Equals(fact) Then
                ptrGameData.LogEvents.Add(New CampaignerLogEvent(ptrSelectedChit.Name & " changed ownership to " & fact.FactionName, ptrGameData.selectedPlayer.PlayerName, ptrGameData.ElapsedTime.ToString, "<none>", EventAuthorityLevel.PublicEvent))
                transferOwner(fact)
            End If

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub transferOwner(ByRef target As Faction)
        Try
            ptrSelectedChit.OwningFaction = target
            '// relist ALL units since we made a big ole change.
            ListChits()
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub mnuAdvantages_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAdvantages.Click
        Try
            Dim f As New camp128c.frmAdvantageEditor
            f.showdialog(ptrSelectedChit)
            ListProperties()

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub mnuPV_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuPV.Click
        Dim f As New camp128c.frmBasicEntry("Enter numeric value", "[0-9]")
        f.ShowDialog()

        Dim oldPv As Single
        Dim newPV As Single

        If ptrSelectedChit.UnitType = GroundChitClass_cgd.Battlegroup Then
            oldPv = DirectCast(ptrSelectedChit, BattlegroupChit).CurrentPV
        Else
            oldPv = DirectCast(ptrSelectedChit, ChitUnitTypeMember).ptrUnitType.SuggestedPointValue
        End If


        Try
            newPV = CSng(f.Data)
            If Not oldPv.Equals(newPV) Then
                '// we set the override value. Whoooohaaa
                If ptrSelectedChit.UnitType = GroundChitClass_cgd.Battlegroup Then
                    DirectCast(ptrSelectedChit, BattlegroupChit).overridePointValue = newPV
                Else
                    DirectCast(ptrSelectedChit, ChitUnitTypeMember).OverridePointValue = newPV
                End If

                '// log this here event
                ptrGameData.LogEvents.Add(New CampaignerLogEvent("Unit " & ptrSelectedChit.Name & " has been changed in point value from: " & oldPv & " to " & newPV, ptrGameData.selectedPlayer.PlayerName, ptrGameData.ElapsedTime.ToString, "<none>", EventAuthorityLevel.PublicEvent))

            End If
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub
End Class

